package com.jysz.certificate.web.action.pm;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.enums.pm.PmUserStype;
import com.jysz.certificate.persistence.model.pm.PmUser;
import com.jysz.certificate.persistence.model.pm.PmUserList;
import com.jysz.certificate.persistence.model.system.Organization;
import com.jysz.certificate.service.pm.PmUserListService;
import com.jysz.certificate.service.pm.PmUserService;
import com.jysz.certificate.service.system.OrganizationService;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/pm/user")
public class PmUserAction extends AbstractBaseAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private PmUserService pmUserService;
	@Resource
	private PmUserListService pmUserListService;
	@Resource
	private OrganizationService organizationService;
	private PmUser pmUser;
	private Long id;
	private PmUser search;
	private Integer state;
	private List<PmUserList> pmUserLists;
	private String msg;
	private String masht;
	private Date addDate;
	
	@Action("openAdd")
	public String openAdd(){
		addDate = new Date();
		Long[] objId = new Long[]{};
		List<Long> longs = new ArrayList<Long>();
		if(state!= null){
			if(null == search){
				search = new PmUser();
				search.setState(PmUserStype.index(state));
			}else{
				search.setState(PmUserStype.index(state));
			}
		}
		List<Organization> organizations =organizationService.UserDomainLists(getUser().getId());
		if(null != organizations && organizations.size()>0){
			msg="";
			for(int i=0;i<organizations.size();i++){
				Organization organization =organizations.get(i);
				boolean bo = pmUserListService.yiBoolean(organization.getParent().getId(), new Date(),getUser());
				if(bo){
					longs.add(organization.getParent().getId());
//					objId[a++]=organization.getParent().getId();
				}else{
					msg += organization.getParent().getName()+",";
					if(getUser().getPost().equals(PostType.Admin)){
						msg = "所有区域";
					}
				}
				
			}
		}
		if(longs.size()>0){
			objId = new Long[longs.size()];
			for(int i=0;i<longs.size();i++){
				objId[i]=longs.get(i);
			}
		}
		pmUserLists = pmUserListService.openAddUserPage(getUser(),objId, getCurrentPage(), getPageSize());
		this.setRedirectUrl("index.do?currentPage=" + getCurrentPage(),"search",search);
		return "openadd";
	}
	/**
	 * 详情
	 * @return
	 */
	@Action("openEdit")
	public String openEdit(){
		 pmUser = pmUserService.find(id);
		this.setRedirectUrl("index.do?currentPage=" + getCurrentPage(),"search",search);
		return "openaddedit";
	}
	@Action(ADD)
	public String add(){
		Long[] objId = new Long[]{};
		List<Long> longs = new ArrayList<Long>();
		if(state!= null){
			if(null == search){
				search = new PmUser();
				search.setState(PmUserStype.index(state));
			}else{
				search.setState(PmUserStype.index(state));
			}
		}
		List<Organization> organizations =organizationService.UserDomainLists(getUser().getId());
		if(null != organizations && organizations.size()>0){
			msg="";
			for(int i=0;i<organizations.size();i++){
				Organization organization =organizations.get(i);
				boolean bo = pmUserListService.yiBoolean(organization.getParent().getId(), new Date(),getUser());
				if(bo){
					longs.add(organization.getParent().getId());
				}else{
					msg += organization.getParent().getName()+",";
				}
				
			}
		}
		if(longs.size()>0){
			objId = new Long[longs.size()];
			for(int i=0;i<longs.size();i++){
				objId[i]=longs.get(i);
			}
		}
		setPage(pmUserService.addUserPage(getUser(),objId, getCurrentPage(), getPageSize(),masht));
		setSuccessPopMessage("数据生成成功");
		this.setRedirectUrl("index.do?currentPage=" + getCurrentPage(),"search",search);
		return SHOW_MSG_PAGE;
	}
	@Action("delete")
	public String delete(){
		 pmUserService.destory(id);
		setSuccessPopMessage("数据删除成功");
		this.setRedirectUrl("index.do?currentPage=" + getCurrentPage(),"search",search);
		return SHOW_MSG_PAGE;
	}
	@Action(INDEX)
	public String index(){
		
		if(state!= null){
			if(null == search){
				search = new PmUser();
				search.setState(PmUserStype.index(state));
			}else{
				search.setState(PmUserStype.index(state));
			}
		}
		setPage(pmUserService.findPage(search, this.getCurrentPage(),this.getPageSize()));
		return INDEX;
	}
	@Action("openPass")
	public String openPass(){
		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM");
		if(state!= null){
			if(null == search){
				search = new PmUser();
				search.setState(PmUserStype.index(state));
			}else{
				search.setState(PmUserStype.index(state));
			}
		}
		PmUser pmUsers = pmUserService.find(id);
		pmUsers.setDeliveryTime(new Date());
		pmUsers.setDeliceryMM(dft.format(new Date()));
		pmUsers.setState(PmUserStype.SUBMITED);
		
		pmUserService.save(pmUsers);
		setSuccessPopMessage("名单发布成功");
		this.setRedirectUrl("index.do?currentPage=" + getCurrentPage(),"search",search);
		return SHOW_MSG_PAGE;
	}
	@Action("ifYouBe")
	public String ifYouBe()throws Exception{
//		msg = new String(msg.getBytes("gb2312"),"gbk");
		msg = java.net.URLDecoder.decode(msg,"UTF-8"); 
		boolean bo = false;
		String msString[] = msg.split(",");
		if(msString.length>0){
			for(int i=0;i<msString.length;i++){
					bo = pmUserService.ifYouBe(getUser().getId(), new Date(),msString[i]);
					if(bo){
						Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
						this.setJsonText(gson.toJson(bo));
					return JSON_PAGE;
					}
			}
			
		}
		
			Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
			this.setJsonText(gson.toJson(bo));
		return JSON_PAGE;
	}
	
	public PmUser getPmUser() {
		return pmUser;
	}
	public void setPmUser(PmUser pmUser) {
		this.pmUser = pmUser;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}public PmUser getSearch() {
		return search;
	}
	public void setSearch(PmUser search) {
		this.search = search;
	}
	public Integer getState() {
		return state;
	}
	public void setState(Integer state) {
		this.state = state;
	}
	public List<PmUserList> getPmUserLists() {
		return pmUserLists;
	}
	public void setPmUserLists(List<PmUserList> pmUserLists) {
		this.pmUserLists = pmUserLists;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getMasht() {
		return masht;
	}
	public void setMasht(String masht) {
		this.masht = masht;
	}
	public Date getAddDate() {
		return addDate;
	}
	public void setAddDate(Date addDate) {
		this.addDate = addDate;
	}

}
